<?php
/**
 * 考勤
 * 打卡，
 * 张名尊， 20140429
 */ 


class attendanceAction extends backendAction{
    
    public function _initialize() {
        parent::_initialize();
		
		$_REQUEST['sort'] = 'workDate';
        $this->_mod = D('attendance');
        $this->_cate_mod = D('region');
    }   
    
    public function _before_index(){
        $this->list_relation = true;
        $this->readyData();
        
    }
    
    
    protected function _search(){
        $map = array();
        ($time_start = $this->_request('time_start', 'trim')) && $map['verifyTime'][] = array('egt', strtotime($time_start));
        ($time_end = $this->_request('time_end', 'trim')) && $map['verifyTime'][] = array('elt', strtotime($time_end)+(24*60*60-1));
        ($verifyType = $this->_request('verifyType', 'trim')) && $map['verifyType'] = $verifyType;   
        if($userName = $this->_request('userName', 'trim'))
        {
            $where['userName'] = array('like', '%'.$userName.'%');           
            $where['realName'] = array('like', '%'.$userName.'%');
            $where['_logic'] = 'or';
            $userMap['_complex'] = $where;
            $userList = M('admin')->where($userMap)->getField('id,userName');
            $userIds = array_keys($userList);
            if(!empty($userIds)){
                $map['userId'] = array('in',$userIds);
            }  
        }
        $admin = getUserSession();
        if(!empty($admin['regionId'])){
            $id_arr = $this->_cate_mod->get_child_ids($admin['regionId'], true);
            $id_arr = M('admin')->where(array('regionId'=>array('in',$id_arr)))->getField('id,userName');

            $id_arr = array_keys($id_arr);   
            if(!empty($id_arr))
            {         
                $map['userId'] = array('in',$id_arr);
            }
        }
         
        $regionId = $this->_request('regionId');
        $selected_ids = '';
        if ($regionId) {
            $regionArr = explode('_',$regionId);
            if(count($regionArr)==2){
              $regionId = $regionArr[0];
              $storeId   = $regionArr[1];  
              $map['storeId']  = $storeId ;
              //$map['regionId'] = $regionId ;
              $spid = $this->_cate_mod->where(array('id'=>$regionId))->getField('spid');
              $selected_ids = $spid ? $spid . $regionId .'|'.$regionId.'_'.$storeId: $regionId.'|'.$regionId.'_'.$storeId;
            }else{
                $id_arr = $this->_cate_mod->get_child_ids($regionId, true);
                //$map['regionId'] = array('IN', $id_arr);
                $id_arr = M('store')->where(array('regionId'=>array('in',$id_arr)))->getField('id,name');
                $id_arr = array_keys($id_arr);
                $map['storeId'] = array('in',$id_arr);                
                $spid = $this->_cate_mod->where(array('id'=>$regionId))->getField('spid');
                $selected_ids = $spid ? $spid . $regionId : $regionId;
            }          
        } 
		
		$admin = getUserSession();
        if(!empty($admin['regionId'])){
            //$this->_cate_mod = D('region');
            $id_arr = D('region')->get_child_ids($admin['regionId'], true);
            $id_arr = M('admin')->where(array('regionId'=>array('in',$id_arr)))->getField('id,userName');

            $id_arr = array_keys($id_arr);   
            if(!empty($id_arr))
            {         
                $map['userId'] = array('in',$id_arr);
            }
        }
      
        $this->assign('search', array(
            'time_start' => $time_start,
            'time_end' => $time_end,           
            'verifyType' => $verifyType,
            'selected_ids' => $selected_ids,   
                    
            'userName'=>$userName,
        ));

        return $map;
    }    
    
    
    
    private function readyData(){
        //考勤审核类型
        $checkVerifyType = C('CHECK_VERIFY_TYPE');
        //请假类型
        $leaveType = C('LEAVE_TYPE');
        //请假审批类型
        $leaveVerifyType = C('LEAVE_VERIFY_TYPE');
        $this->assign('leaveVerifyType',$leaveVerifyType);
        $this->assign('leaveType',$leaveType);
        $this->assign('checkVerifyType',$checkVerifyType);
           
    }
    
	/**
	 * 打卡记录审核结果提交
	 */
	public function checkVerifySubmit(){
		$checkId = I('checkId'); //打卡记录id
		
		$data['verifyType']		= I('verifyTypeId'); //审核结果类型id
		$data['verifyRemark']	= I('verifyRemark'); //审核结果备注
		$data['verifyTime']		= time();	//审核时间
		if($checkId && $data['verifyType'] && D('attendance')->where(array('id'=>$checkId))->save($data)){
		//	$this->ajaxReturn(200, L('success'));
            $this->success(L('operation_success'));
		}else{
		//	$this->ajaxReturn(300, L('error'));
            $this->error(L('operation_failure'));
		}
	}
    
    public function checkVerify(){
        $checkId = I('checkId'); //打卡记录id
        $this->assign('checkId',$checkId);
        $this->assign('attendance',D('attendance')->getVerifyOptions());
        $this->display();
    }
	
	public function photo(){
		$path = I('path');
		
		$this->assign('path',$path);
        $response = $this->fetch();
        $this->ajaxReturn(1, '', $response);
	}
	
}


?>